package com.linkoog.devtools.code_generator.database.sqlparser;

import com.linkoog.devtools.code_generator.database.domain.TableInfo;
import com.linkoog.devtools.code_generator.database.sqlparser.parser.MysqlSqlParser;
import com.linkoog.devtools.code_generator.database.sqlparser.parser.PostgreSqlParser;
import com.linkoog.devtools.code_generator.database.sqlparser.parser.SqlParser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.List;

public class SqlParserTest {


    public static String readFileContext(File file){
        StringBuilder result = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));
            String s = null;
            while((s = br.readLine())!=null){
                result.append(System.lineSeparator()+s);
            }
            br.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return result.toString();
    }


    public static void main(String[] args) {
        ParseResult parseResult = null;

        parseResult = parseMysql();
        //parseResult = parsePostgre();


        if (ParseState.SUCCESS == parseResult.getParseState()){
            List<TableInfo> tableInfos = parseResult.getTableInfos();
            for (TableInfo tableInfo : tableInfos) {
                System.out.println(tableInfo);
            }
        }
    }

    private static ParseResult parseMysql(){
        File file = new File("D:/mysql.txt");
        String ddlSql = readFileContext(file);
        SqlParser sqlParser = new MysqlSqlParser();
        ParseResult parseResult =  sqlParser.parseCreateTable(ddlSql);
        return parseResult;
    }

    private static ParseResult parseOracle(){
        File file = new File("D:/oracle.txt");
        String ddlSql = readFileContext(file);
        SqlParser sqlParser = new MysqlSqlParser();
        ParseResult parseResult =  sqlParser.parseCreateTable(ddlSql);
        return parseResult;
    }


    private static ParseResult parsePostgre(){
        File file = new File("D:/postgre.txt");
        String ddlSql = readFileContext(file);
        SqlParser sqlParser = new PostgreSqlParser();
        ParseResult parseResult =  sqlParser.parseCreateTable(ddlSql);
        return parseResult;
    }


}
